package servlet;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;

import util.DbUtil;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//1.接收网页上表单的数据
		String cid=request.getParameter("id");
		String cname=request.getParameter("cname");
		String remark=request.getParameter("remark");
		//2.连接数据库，修改数据
		Connection conn=null;
		PreparedStatement pstmt=null;
		conn=(Connection) DbUtil.getConnection();
		String sql="UPDATE category SET name=?,remark=? WHERE id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, cname);
			pstmt.setString(2, remark);
			pstmt.setString(3, cid);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbUtil.backConn(conn, pstmt, null);
		}
		response.sendRedirect("FindThingServlet");

	}
    @Override
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
